<input type="hidden" id="title" value="{{title}}" />
<div class="modal-body">
	<form>
		<div class="row">
			<div class="col-md-6 pr-0">
				<div class="form-group form-floating-label">
					<input id="name" type="text" class="form-control input-border-bottom" required="" >
					<label for="inputFloatingLabel" class="placeholder">产品名称</label>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group form-floating-label">
					<select class="form-control input-border-bottom" id="tags" required="">
						{%for i, v in ipairs(result_tags) do%}
							<option value="{*v.id*}">{*v.name*}</option>
						{%end%}
					</select>
					<label for="selectFloatingLabel" class="placeholder">类别</label>
				</div>
			</div>
			<div class="col-md-6 pr-0">
				<div class="form-group form-floating-label">
					<input id="cpu" type="text" class="form-control input-border-bottom" required="">
					<label for="inputFloatingLabel" class="placeholder">CPU</label>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group form-floating-label">
					<input id="memory" type="text" class="form-control input-border-bottom" required="">
					<label for="inputFloatingLabel" class="placeholder">内存</label>
				</div>
			</div>
			<div class="col-md-6 pr-0">
				<div class="form-group form-floating-label">
					<select class="form-control input-border-bottom" id="system" required="">
						<option value="2">Linux</option>
						<option value="1">Windows</option>
					</select>
					<label for="selectFloatingLabel" class="placeholder">操作系统</label>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group form-floating-label">
					<input id="concurrent" type="text" class="form-control input-border-bottom" required="">
					<label for="inputFloatingLabel" class="placeholder">并发</label>
				</div>
			</div>
			<div class="col-md-6 pr-0">
				<div class="form-group form-floating-label">
					<select class="form-control input-border-bottom" id="network_type" required="" onchange="changeVal();">
						<option value="2">按需</option>
						<option value="1">固定带宽</option>
						<option value="3">无需外网</option>
					</select>
					<label for="selectFloatingLabel" class="placeholder">网络类型</label>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group form-floating-label">
					<input id="network_size" type="text" class="form-control input-border-bottom" required="">
					<label for="inputFloatingLabel" class="placeholder">带宽大小M</label>
				</div>
			</div>
			
			<div class="col-sm-12">
				<div class="form-group form-floating-label">
					<textarea id="desc" class="form-control input-border-bottom"></textarea>
					<label for="inputFloatingLabel" class="placeholder">描述</label>
				</div>
			</div>
			<div class="form-group">
				<label class="form-label">依赖模块：</label>
				<div class="selectgroup selectgroup-pills">
					{%for i, v in ipairs(result) do%}
						<label class="selectgroup-item">
							<input type="checkbox" name="ck_rel" value="{*v.id*}" class="selectgroup-input">
							<span class="selectgroup-button">{*v.name*}</span>
						</label>
					{%end%}
				</div>
			</div>
		</div>
	</form>
</div>
<div class="modal-footer no-bd">
	<button type="button" id="" onclick="addInfo();" class="btn btn-primary">提交</button>
	<button type="button" id="btnClose" class="btn btn-danger" data-dismiss="modal">取消</button>
</div>
<script>
	$('#myModalLabel').text($('#title').val());
	$('#name').focus();

	function changeVal(){
		if($("#network_type").val() == 3){
			$("#network_size").val(0);
		}
	}

	function addInfo(){
		$('#myModal').modal('hide');
		var ids = [];
		$("input[name='ck_rel']:checked").each(function() { 
			ids.push($(this).val());
		});
		var params = {
			"ids":ids,
			"name":$("#name").val(),
			"cpu":$("#cpu").val(),
			"memory":$("#memory").val(),
			"system":$("#system").val(),
			"concurrent":$("#concurrent").val(),
			"network_type":$("#network_type").val(),
			"network_size":$("#network_size").val(),
			"tags":$("#tags").val(),
			"desc":$("#desc").val()
		};
		$.ajax({
			type: "POST",
			url: "/product/add",
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify(params),
			dataType: "json",
			// async:false,
			success:function(data){
				if(data.success == false){
					swal("添加失败!", "原因："+data.data+"!", {
						icon : "error",
						buttons: {
							confirm: {
								className : 'btn btn-danger'
							}
						},
					});
				}else{
					swal("【"+$("#name").val()+"】添加成功！", {
						icon : "success",
						visible: false,
						buttons: false,
						timer: 3000,
					}).then(function(){
						toMune('/product/query');
					});
				}
			},
			error:function(){
				alert("error");
			}
		});	
	}
</script>